﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using TrainingWorkshop.Models;
using System.Text.RegularExpressions;
using System.ServiceModel;
using TrainingWorkshop.Contracts;
namespace TrainingWorkshop.Presentation
{
    public partial class CourseManage : System.Web.UI.Page
    {
        /// <summary>
        /// PageLoad Show all the Course Records int the gridview
        /// </summary>
        /// <param name="sender">object sender</param>
        /// <param name="e">EventArgs e</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridviewBind();
            }
        }

        /// <summary>
        /// Select the required records and show them in the gridview
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">EvenArgs e</param>
        protected void SearchButton_Click(object sender, EventArgs e)
        {
            //judge the coursename
            string courseName = CourseNameTextBox.Text.Trim();
            string regexString = "^[A-Za-z]+$";
            Regex regex = new Regex(regexString);
            if (courseName == "")
                WarningMessageLabel.Text = "Not null";
            if (courseName.Length > 10)
                WarningMessageLabel.Text = "CourseName too long";
            if (!regex.Match(courseName).Success)
                WarningMessageLabel.Text = "Please input proper CourseName";
            else
            {
                WarningMessageLabel.Text = "";
                GridviewBind();
            }
        }

        /// <summary>
        /// CourseListGridView_RowDataBound bound data to dropdownlist when editing
        /// </summary>
        /// <param name="sender">Object Sender</param>
        /// <param name="e">GridViewRowEventArgs e</param>
        protected void CourseListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("ProjectBEndpoint"))
            {
                IProjectBService iProjectBService = channelFactory.CreateChannel();
                using (iProjectBService as IDisposable)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        //Bind Javascript to confirm dialog
                        if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                        {
                            Label labelStatus = (Label)e.Row.FindControl("StatusLabel");
                            if (labelStatus.Text.Trim().ToLower() == "closed")
                                ((Button)e.Row.FindControl("ButtonDelete")).Attributes.Add("onclick", "return confirm('Are you sure to delete this?')");
                            else
                                ((Button)e.Row.FindControl("ButtonDelete")).Attributes.Add("onclick", "alert('Open course is not allowed deleted！');return false;");
                        }

                    }
                }
            }
        }


        /// <summary>
        /// RowDeleting delete the selected coloum based on the Id
        /// </summary>
        /// <param name="sender">object sender</param>
        /// <param name="e">GridViewDeleteEventArgs e</param>
        protected void CourseListGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int deleteId = Convert.ToInt32(CourseListGridView.DataKeys[e.RowIndex].Value.ToString());
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("ProjectBEndpoint"))
            {
                IProjectBService iProjectBService = channelFactory.CreateChannel();
                using (iProjectBService as IDisposable)
                {
                    iProjectBService.DeleteCourse(deleteId);
                }
            }
            GridviewBind();

        }


        /// <summary>
        /// EditCancel change the column patten
        /// </summary>
        /// <param name="sender">object sender</param>
        /// <param name="e">GridViewCancelEditEventArgs e</param>
        protected void CourseListGridView_Cancel(object sender, GridViewCancelEditEventArgs e)
        {
            this.CourseListGridView.EditIndex = -1;//change from edit to scan
            GridviewBind();

        }

        /// <summary>
        /// ChangeStatus batch change open to close
        /// </summary>
        /// <param name="sender">object sender</param>
        /// <param name="e">EventArgs e</param>
        protected void ChangeStatusButton_Click(object sender, EventArgs e)
        {
            //List<Models.Course> courseList = new List<Course>();
            //for (int i = 0; i < CourseListGridView.Rows.Count; i++)
            //{
            //    CheckBox checkbox = (CheckBox)CourseListGridView.Rows[i].FindControl("StatusCheckBox");
            //    string statusLabelValue = ((Label)CourseListGridView.Rows[i].FindControl("StatusLabel")).Text.Trim();
            //    if (checkbox.Checked == true && statusLabelValue == "Open")
            //    {
            //        Course course = new Course();
            //        course.CourseId = Convert.ToInt32(CourseListGridView.DataKeys[i].Value);
            //        course.CourseStatus = true;
            //        courseList.Add(course);
            //    }
            //}
            //using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("ProjectBEndpoint"))
            //{
            //    IProjectBService iProjectBService = channelFactory.CreateChannel();
            //    using (iProjectBService as IDisposable)
            //    {
            //        iProjectBService.ChangeState(courseList);
            //    }
            //}
            GridviewBind();
        }
        /// <summary>
        /// AllCourses 
        /// Show all the courses in database
        /// </summary>
        /// <param name="sender">object sender</param>
        /// <param name="e">EventArgs e</param>
        protected void AllCoursesCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (AllCoursesCheckBox.Checked)
            {
                AllCoursesCheckBox.Checked = false;
                CourseNameTextBox.Text = "";
                StatusRadioButtonList.SelectedValue = "True";
                GridviewBind();
            }
        }
        /// <summary>
        /// GridviewBind
        /// Bind the data to gridview in different situations
        /// </summary>
        protected void GridviewBind()
        {

            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("ProjectBEndpoint"))
            {
                IProjectBService iProjectBService = channelFactory.CreateChannel();
                using (iProjectBService as IDisposable)
                {
                    if (CourseNameTextBox.Text == "")
                    {
                        CourseListGridView.DataSource = iProjectBService.GetAllCourses();
                        CourseListGridView.DataKeyNames = new string[] { "CourseId" };
                        CourseListGridView.DataBind();
                    }
                    else
                    {
                        CourseListGridView.DataSource = iProjectBService.GetAllSelectedCourses(CourseNameTextBox.Text, Convert.ToBoolean(StatusRadioButtonList.SelectedValue));
                        CourseListGridView.DataKeyNames = new string[] { "CourseId" };
                        CourseListGridView.DataBind();
                    }
                }
            }



        }
    }
}
